<template>
  <div>
    <back></back>
    <form-back height="500px">
      <public-form-body ref="register_form" :user="user" :rules="rules">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="user.email" placeholder="请输入邮箱" clearable/>
          <el-link @click="sendCode" :underline="false" type="primary" :disabled="timer.disable"
                   style="margin-top: 0; line-height: 20px">
            {{ timer.text[timer.disable ? 1 : 0] }}
            <span v-show="timer.disable">({{ timer.num }})</span>
          </el-link>
        </el-form-item>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="user.name" placeholder="请输入用户名" clearable/>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="user.code" placeholder="请输入验证码" clearable/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="user.password" placeholder="请输入密码" type="password" show-password clearable/>
        </el-form-item>
        <el-form-item label="确认密码" prop="repass">
          <el-input v-model="user.repass" placeholder="请输入确认密码" type="password" show-password clearable/>
        </el-form-item>
        <div id="button">
          <el-button type="primary" size="small" @click="register">注册</el-button>
          <el-button type="primary" size="small" @click="resetForm">重置</el-button>
        </div>
        <el-link :underline="false" type="primary" @click="$router.push('login')">已有帐号？去登录</el-link>
      </public-form-body>
    </form-back>
  </div>
</template>

<script>
import {sendCode} from "../util/request/email";
import {register} from "../util/request/user";
import {encrypt} from "../util/encrypt";
import {message} from "../util/message";
import Back from "../components/back";
import FormBack from "../components/form-back";
import PublicFormBody from "../components/public-form-body";

export default {
  name: "register",
  components: {PublicFormBody, FormBack, Back},
  data() {
    let verifyPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.user.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback()
      }
    }
    return {
      user: {
        email: '',
        name: '',
        code: '',
        password: '',
        repass: ''
      },
      rules: {
        email: [
          {required: true, message: '请输入邮箱地址', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur'},
        ],
        name: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 8, max: 20, message: '密码长度 8 到 20 位', trigger: 'blur'}
        ],
        repass: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 8, max: 20, message: '密码长度 8 到 20 位', trigger: 'blur'},
          {validator: verifyPass, trigger: 'blur'},
        ]
      },
      timer: {
        num: 60,
        text: ['发送验证码', '已发送'],
        disable: false,
        interval: '',
      },
    }
  },

  watch: {
    'timer.num'() {
      if (this.timer.num === 0) {
        clearInterval(this.timer.interval)
        this.timer.num = 60
        this.timer.disable = false
      }
    }
  },
  methods: {
    resetForm() {
      this.$refs.register_form.resetFields();
    },
    register() {
      this.$refs.register_form.validate((valid) => {
        if (valid) {
          register({
            email: this.user.email,
            password: encrypt(this.user.password),
            name: this.user.name,
            code: this.user.code
          }).then(res => {
            let msg = message(res.data.success, res.data.msg);
            this.$message(msg)
          })
        }
      });
    },
    sendCode() {
      if (this.user.email === '') {
        return
      }
      this.timer.disable = true
      this.timer.interval = setInterval(() => {
        this.timer.num--
      }, 1000)
      sendCode({
        email: this.user.email,
        type: 0
      }).then(res => {
        this.$message(message(res.data.success, res.data.msg))
      })
    }
  }
}
</script>

<style scoped>

</style>
